import { _decorator, Component, Enum, Label } from 'cc';
import { GameManager } from '../GameManager';
import { EventManager, MyEvent } from '../../AshFramework/Managers/EventManager';
const { ccclass, property } = _decorator;

export enum LABELTYPE {
    ENERGY,
    HP,
}

@ccclass('UILabel')
export class UILabel extends Component {

    @property({ type: Enum(LABELTYPE) })
    type: LABELTYPE = LABELTYPE.ENERGY;

    private _label: Label = null;
    protected onLoad(): void {
        this._label = this.getComponent(Label);
    }

    updateEnergy() {
        // GameManager.Instance.userData.epoch
        if (this.type != LABELTYPE.ENERGY) return;
        if (GameManager.Instance.userData.energy < 1) {
            this._label.string = GameManager.Instance.userData.energy.toPrecision(2);
        } else {
            this._label.string = GameManager.Instance.userData.energy.toPrecision(3);
        }
    }

    updateHp() {
        if (this.type != LABELTYPE.HP) return;
        this._label.string = String(GameManager.Instance.userData.hp);
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.ENERGY, this.updateEnergy, this);
        EventManager.on(MyEvent.HP, this.updateHp, this);
    }
    protected onDisable(): void {
        EventManager.off(MyEvent.ENERGY, this.updateEnergy, this);
        EventManager.off(MyEvent.HP, this.updateHp, this);
    }
}


